package com.sincebest.fivestar.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.sincebest.fivestar.common.exception.RobotErrors;
import com.sincebest.fivestar.mapper.RobotMapper;
import com.sincebest.fivestar.model.Robot;
import com.sincebest.fivestar.model.RobotWxGroupRela;
import com.sincebest.fivestar.model.WechatGroupMember;
import com.sincebest.fivestar.rest.GameOmsClient;
import com.sincebest.fivestar.service.RobotService;

/**
 * 充值服务
 * 
 * @author peter
 *
 */
@Service
public class RobotServiceImpl implements RobotService {

	private static final Logger log = LoggerFactory.getLogger(RobotServiceImpl.class);

	@Autowired
	private RobotMapper robotMapper;

	@Autowired
	private GameOmsClient gameOmsClient;

	@Override
	public Integer bindingRobot(String imId, Integer robotId) {
		if (null == robotId) {
			Robot robot = robotMapper.getRobotByImId(imId);
			if (null == robot) {
				int record = robotMapper.addRobot(imId);
			}
		} else {
			int record = robotMapper.updateRobot(robotId, imId);
			if (record == 0) {
				throw new RobotErrors().NO_THIS_ROBOT.bornException();
			}
		}
		Robot robot = robotMapper.getRobotByImId(imId);
		return robot.getId();
	}

	@Override
	public Boolean verifyIsHouseOwner(String houseId, String wxId) {
		return gameOmsClient.verifyIsHouseOwner(houseId, wxId);
	}

	@Override
	public Integer bindingWxGroup(Integer robotId, String groupId, String ownerWxId) {
		RobotWxGroupRela rela = robotMapper.selectRobotWxGroupRela(groupId);
		int result = 0;
		try {
			if (null != rela) {
				if (rela.getRobotId() != robotId) {
					rela.setRobotId(robotId);
				}
				if (!rela.getGroupOwnerId().equals(ownerWxId)) {
					rela.setGroupOwnerId(ownerWxId);
				}
				result = robotMapper.updateWxGroupRela(rela);
			} else {
				result = robotMapper.addRobotWxGroupRela(robotId, groupId, ownerWxId, null);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw RobotErrors.BINDING_ERROR.bornException();
		}

		return result;
	}

	@Override
	public Integer bindingHouse(String groupId, String houseId) {
		RobotWxGroupRela rela = new RobotWxGroupRela();
		rela.setWeChatGroupId(groupId);
		rela.setHouseId(houseId);
		int result = 0;
		try {
			result = robotMapper.updateWxGroupRela(rela);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw RobotErrors.BINDING_ERROR.bornException();
		}
		return result;
	}

	@Override
	public RobotWxGroupRela queryWxGroupRela(String groupId) {
		try {
			RobotWxGroupRela rela = robotMapper.selectRobotWxGroupRela(groupId);
			return rela;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw RobotErrors.QUERY_GROUP_ROBOT_RELA_ERROR.bornException();
		}
	}

	@Override
	public String addWxGroupMembers(List<WechatGroupMember> members) {
		try {
			robotMapper.addWxGroupMembers(members);
			// 一次加一个人的时候才返回邀请链接
			if (members.size() == 1) {
				WechatGroupMember firstMember = members.get(0);
				String wxGroupId = firstMember.getWeChatGroupId();
				RobotWxGroupRela rela = robotMapper.selectRobotWxGroupRela(wxGroupId);
				String inviteUrl = gameOmsClient.getGameInviteLink(rela.getHouseId());
				return inviteUrl;
			}
			return null;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw RobotErrors.ADD_MEMBER_ERROR.bornException();
		}
	}

	@Override
	public String deleteWxGroupMembers(List<WechatGroupMember> members) {
		WechatGroupMember firstMember = members.get(0);
		WechatGroupMember member = robotMapper.getWxGroupMemberInfo(firstMember.getMemberId());
		try {
			robotMapper.deleteWxGroupMembers(members, firstMember.getWeChatGroupId());
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw RobotErrors.DELETE_MEMBER_ERROR.bornException();
		}
		// 一次加一个人的时候才返回邀请链接
		if (members.size() == 1) {

			String wxGroupId = firstMember.getWeChatGroupId();
			RobotWxGroupRela rela = robotMapper.selectRobotWxGroupRela(wxGroupId);
			if (null == rela) {
				throw RobotErrors.GROUP_NOT_BINDING_ROBOT_ERROR.bornException();
			}
			String inviteUrl = gameOmsClient.getGameKickLink(rela.getHouseId(), member.getHouseUserId(),
					member.getNickname(), member.getAvatar());
			return inviteUrl;
		}
		return null;

	}

	@Override
	public void updateWxGroupMemberInfo(WechatGroupMember member) {
		if (StringUtils.isEmpty(member.getMemberId())) {
			throw RobotErrors.MEMBER_WXID_EMPTY_ERROR.bornException();
		}
		try {
			robotMapper.updateGroupMemberInfo(member);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw RobotErrors.MODIFY_MEMBER_INFO_ERROR.bornException();
		}
	}

	@Override
	public void updateWxGroupMemberWxId(String oldMemberId, String newMemberId) {
		if (StringUtils.isEmpty(oldMemberId) || StringUtils.isEmpty(newMemberId)) {
			throw RobotErrors.MEMBER_WXID_EMPTY_ERROR.bornException();
		}
		try {
			robotMapper.updateGroupMemberWxId(oldMemberId, newMemberId);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw RobotErrors.MODIFY_MEMBER_INFO_ERROR.bornException();
		}
	}

}
